<?php

class ProductCreateController extends BaseController
{
	//前台预览产品SEO信息
	public $pageTitle = "产品预览";
	public $pageKeyword = "产品预览";
	public $pageDesc = "产品预览";
	public $active_cur = "pro_list_8";

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			//'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * 发布产品
	 */
	public function actionCreate()
	{
		$step = (int)$_GET['step'];
		switch($step){
			case 1;
					if(isset($_GET['next'])){
						//没有选择一级分类，或没有选择二级分类时，跳转到错误页面。
						if((int)($_GET['cateid3'])==0){
							if((int)$_GET['cateid2']==0)
								throw new CHttpException(404,'');
						}
						//选择三级分类时，取三级分类，没有选择三级分类时，取二级分类。
						$categoryid = (int)($_GET['cateid3'])?(int)($_GET['cateid3']):(int)($_GET['cateid2']);
						
						//将选择的ID存入coolkie下次打开此页面记住
						$cookie = new CHttpCookie('categoryid_remember',$categoryid);
						$cookie->expire = time()+60*60*24*30;  //有限期30天
						Yii::app()->request->cookies['categoryid_remember']=$cookie;
						
						$this->redirect(array($this->pre_module_url.'/productCreate/create/step/2','id'=>$categoryid));
					}
					if(isset($_GET['zam_title'])){
						$title = trim($_GET['zam_title']);
						$model = new Category();
						$model->unsetAttributes();
						$model->zca_title = $title;
						$model->zca_level = '>1';

						$this->render('product_one',array(
								'model'=>$model,
						));
						exit;
					}
					//读取cookie 记忆上次选择的行业分类
					$cookie = Yii::app()->request->getCookies();
					$remember_cateid =  ($cookie['categoryid_remember'])?$cookie['categoryid_remember']->value:102;
					$this->render('product_one',array(
							'remember_cateid'=>$remember_cateid,
					));
					break;
			case 2;
					
					//实例化产品模型
					$model=new Product;
					$model->zca_id = (int)$_GET['id'];
					
					//提交发布产品操作
					if(isset($_POST['Product']))
					{
						//事务开始
						$transaction= Yii::app()->db->beginTransaction();
						
						//保存产品基本信息
						$model->attributes=$_POST['Product'];
						$model->zp_draft = strtotime($model->zp_draft);//尝试在product模型的方法beforeSave()转换无效，故直接写这
						foreach($_POST['proimage']['url'] as $val){//产品图片取按顺序上传的第一个
							if(!empty($val)){
								$model->zp_pic = $val;
								break;
							}
						}
						//$model->zp_pic = $_POST['proimage']['url'][0];//产品默认图片
						
						if(!$model->save()){
							$error[] = 'product save fail,';
						}
						
						//保存产品属性
						if(isset($_POST['ProAttribute'])){
							$att = $_POST['ProAttribute'];
							foreach($att as $key=>$val){
								if(is_array($val)){
									//如果属性有多个值，分开单条保存
									foreach($val as $key2=>$val2){
										$proAtt = new ProAttribute;
										$proAtt->zp_id = $model->zp_id;//产品ID
										$proAtt->zpa_type = '1';		//类型为1属性
										$proAtt->za_id = $key;			//公用规格ID
										$proAtt->zpa_val = $val2;		//规格值
										if(!$proAtt->save()){
											$error[] = "ProAttribute $val2 save fail,";
										}
									}
								} else{
									//$val = serialize($val);
									$proAtt = new ProAttribute;
									$proAtt->zp_id = $model->zp_id;//产品ID
									$proAtt->zpa_type = '1';		//类型为1属性
									$proAtt->za_id = $key;			//公用规格ID
									$proAtt->zpa_val = $val;		//规格值
									if(!$proAtt->save()){
										$error[] = "ProAttribute $val save fail,";
									}
								}
							}
						}

						//保存产品规格
						if(isset($_POST['sta'])){
							$sta = $_POST['sta'];
							foreach($sta as $key=>$val){
								if(is_array($val)){
									//如果规格有多个值，分开单条保存
									foreach($val as $key2=>$val2){
										//$val = serialize($val);
										$proAtt = new ProAttribute;
										$proAtt->zp_id = $model->zp_id;//产品ID
										$proAtt->zpa_type = '2';		//类型为1属性
										$proAtt->za_id = $key;			//公用规格ID
										$proAtt->zpa_val = $val2;		//规格值
										$proSta->zps_photo = (isset($_POST['stacolor'][$key]))?'2':'1';
										//保存规格图片
										if(isset($_POST['stacolor'][$key][$key2])){
											$proAtt->zpa_pic = $_POST['stacolor'][$key][$key2];
											$proSta->zps_photo ='2';
										}
										if(!$proAtt->save()){
											$error[] = "ProAttribute $val2 save fail,";
										}
									}
								}else{
									//$val = serialize($val);
									$proAtt = new ProAttribute;
									$proAtt->zp_id = $model->zp_id;//产品ID
									$proAtt->zpa_type = '2';		//类型为1属性
									$proAtt->za_id = $key;			//公用规格ID
									$proAtt->zpa_val = $val;		//规格值
									if(!$proAtt->save()){
										$error[] = "ProAttribute $val save fail,";
									}
								}
							}

							//保存规格组合
							$attsta = $_POST['attsta'];
							//重新排序
							sort($sta);
							if(count($sta)==2){
								//有两组规格的时候
								$list[]='';
								$i=0;
								foreach($sta as $key=>$val){
									$sta[$i] = $val;
									$i++;
								}
								foreach($sta[0] as $k1=>$v1){
									foreach($sta[1] as $k2=>$v2 ){
										$list[] = $v1.','.$v2; 
										$list[] = $v2.','.$v1;
									}
								}
								$num = count($attsta['val']);
								for($i=0;$i<$num;$i++){
									if(in_array($attsta['val'][$i],$list)){
										$proSta = new ProAttribute;
										$proSta->zp_id = $model->zp_id;//产品ID
										$proSta->zpa_type = '3';		//类型为3规格组合
										$proSta->zpa_val = $attsta['val'][$i];		//规格值
										$proSta->zpa_stock = $attsta['num'][$i];
										$proSta->zpa_pro_no = $attsta['no'][$i];
										$proSta->zps_md5 = md5($model->zp_id.$attsta['val'][$i]);
										if(!$proSta->save()){
											$error[] = "ProSta ".$proSta->zpa_val." save fail,";
										}
										
										//保存产品价格
										$proSpe = new ProSpe;
										$proSpe->zp_id = $model->zp_id;//产品ID
										$proSpe->zpa_id = $proSta->zpa_id;		//规格记录ID
										$proSpe->zps_price = $attsta['price'][$i];	//价格
										if(!$proSpe->save()){
											$error[] = "ProSpe ".$attsta['price'][$i]." save fail,";
										}
									}
								}
							}elseif(isset($sta[0])){
								//只有一组规格
								$list=$sta[0];
								$num = count($attsta['val']);
								for($i=0;$i<$num;$i++){
									if(in_array($attsta['val'][$i],$list)){
										$proSta = new ProAttribute;
										$proSta->zp_id = $model->zp_id;//产品ID
										$proSta->zpa_type = '3';		//类型为3规格组合
										$proSta->zpa_val = $attsta['val'][$i];		//规格值
										$proSta->zpa_stock = $attsta['num'][$i];
										$proSta->zpa_pro_no = $attsta['no'][$i];
										if(!empty($sta['color'][$i])){
											$proSta->zps_photo ='2';
										}
										$proSta->zps_md5 = md5($model->zp_id.$proSta->zpa_val);
										if(!$proSta->save()){
											$error[] = "ProSta ".$proSta->zpa_val." save fail,";
										}
							
										//保存产品价格
										$proSpe = new ProSpe;
										$proSpe->zp_id = $model->zp_id;//产品ID
										$proSpe->zpa_id = $proSta->zpa_id;		//规格记录ID
										$proSpe->zps_price = $attsta['price'][$i];	//价格
										if(!$proSpe->save()){
											$error[] = "ProSpe ".$attsta['price'][$i]." save fail,";
										}
									}
								}
							}
						}
						//如果使用产品积分系统
						if(F::setting('pointsys')=='2'){
							//保存积分体系
							if(isset($_POST['ProPoint'])){
								foreach($_POST['ProPoint'] as $key=>$val){
									$propoint = new ProPoint();
									$propoint->zp_id = $model->zp_id;//产品ID
									$propoint->zmt_id = $key;//会员组ID
									$propoint->zpn_point = $val;//赠送积分
									if(!$propoint->save()){
										$error[] = "propoint $val save fail,";
									}
								}
							}
						}
						//保存产品图片
						$image = $_POST['proimage'];
						foreach($image['url'] as $key=>$val){
							if(!empty($val)){
								$proImages = new ProImages;
								$proImages->zp_id = $model->zp_id;//产品ID
								$proImages->zpg_url = $val;
								$proImages->zpg_listorder = $key;
								$proImages->zta_id =  $image['id'][$key];
								if(!$proImages->save()){
									$error[] = "proimage $val save fail,";
								}
							}
						}
						
						//保存自定义分类信息
						if(isset($_POST['companyCat'])){
							$cmpcat = $_POST['companyCat'];
						}else{
							//如果没选择自定义分类，则保存为未分类产品
							$cmpcat = array('0');
						}
						foreach($cmpcat as $val){
							$cmpcat = new CmpCatlog;
							$cmpcat->zpc_id = $val;
							$cmpcat->zm_id = Yii::app()->user->userid;
							$cmpcat->zp_id = $model->zp_id;//产品ID
							$cmpcat->inputtime = time();
							if(!$cmpcat->save()){
								$error[] = "cmpcat $val save fail,";
							}
						}
						if(empty($error)){
							//提交事务
							$transaction->commit();
							$this->render('../index/success',array(
									'message'=>'恭喜，宝贝信息发布成功',
									'links'=>array(
											array('继续发布产品',$this->pre_module_url.'/productCreate/create/step/1'),
											array('浏览发布的产品','/home/product/detail/id/'.$model->zp_id),
									),
							));
							exit;
						}else{
							//回滚事务
							$transaction->rollback();
							$this->render('../index/error',array(
									'message'=>'对不起，宝贝信息发布失败',
									//'errorlog'=>join(',',$error),
									'links'=>array(
											array('继续发布产品',$this->pre_module_url.'/productCreate/create/step/1'),
											array('浏览发布的产品','/home/product/detail/id/'.$model->zp_id),
									),
							));
						}
					}
			
					$this->render('product_tow',array(
						   'model'=>$model,
					));
					break;
			default:
					break;
		}
	}

	/**
	 * 修改产品
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
		
		if(isset($_GET['step'])&&$_GET['step']=='1'){
				if(isset($_GET['next'])){
					//没有选择一级分类，或没有选择二级分类时，跳转到错误页面。
					if((int)($_GET['cateid3'])==0){
						if((int)$_GET['cateid2']==0)
							throw new CHttpException(404,'The requested page does not exist.');
					}
					//选择三级分类时，取三级分类，没有选择三级分类时，取二级分类。
					$categoryid = (int)($_GET['cateid3'])?(int)($_GET['cateid3']):(int)($_GET['cateid2']);
				
					//将选择的ID存入coolkie下次打开此页面记住
					$cookie = new CHttpCookie('categoryid_remember',$categoryid);
					$cookie->expire = time()+60*60*24*30;  //有限期30天
					Yii::app()->request->cookies['categoryid_remember']=$cookie;
					
					$model->zca_id = $categoryid;
					$model->save();
					$this->redirect(array($this->pre_module_url.'/productCreate/update','id'=>$id));
				}
				if(isset($_GET['zam_title'])){
					$title = trim($_GET['zam_title']);
					$model = new Category();
					$model->unsetAttributes();
					$model->zca_title = $title;
					$model->zca_level = '>1';
				
					$this->render('product_one',array(
							'model'=>$model,
					));
					exit;
				}
				//读取cookie 记忆上次选择的行业分类
				$cookie = Yii::app()->request->getCookies();
				$remember_cateid =  ($cookie['categoryid_remember'])?$cookie['categoryid_remember']->value:102;
				$this->render('product_one',array(
						'remember_cateid'=>$remember_cateid,
				));
				exit;
		}

		if(isset($_POST['Product']))
		{
						//保存产品数据 ，事务开始
						$transaction= Yii::app()->db->beginTransaction();
						
						try
						{
									//原产品图片
									$pre_pic = $model->zp_pic;
									//保存产品基本信息
									$model->attributes=$_POST['Product'];
									foreach($_POST['proimage']['url'] as $val){//产品图片取按顺序上传的第一个
										if(!empty($val)){
											$model->zp_pic = $val;
											break;
										}
									}
									//$model->zp_pic = $_POST['proimage']['url'][0];//产品默认图片
						
									if(!$model->save()){
										$error[] = 'product save fail,';
									}
									//产品主图变动时更新其它关联表调用的产品图片
									if($model->zp_pic != $pre_pic)
										$model->updateRelatePic($pre_pic,$model->zp_pic);
									
									//删除旧的产品规格属性，库存记录
									$criteria=new CDbCriteria;
									$criteria->compare('zp_id',$model->zp_id);
									ProAttribute::model()->deleteAll($criteria);
									//删除旧的价格记录
									ProSpe::model()->deleteAll($criteria);
									//保存产品属性
									if(isset($_POST['ProAttribute'])){
										$att = $_POST['ProAttribute'];
										foreach($att as $key=>$val){
											if(is_array($val)){
												//如果属性有多个值，分开单条保存
												foreach($val as $key2=>$val2){
													$proAtt = new ProAttribute;
													$proAtt->zp_id = $model->zp_id;//产品ID
													$proAtt->zpa_type = '1';		//类型为1属性
													$proAtt->za_id = $key;			//公用规格ID
													$proAtt->zpa_val = $val2;		//规格值
													if(!$proAtt->save()){
														$error[] = "ProAttribute $val2 save fail,";
													}
												}
											} else{
												//$val = serialize($val);
												$proAtt = new ProAttribute;
												$proAtt->zp_id = $model->zp_id;//产品ID
												$proAtt->zpa_type = '1';		//类型为1属性
												$proAtt->za_id = $key;			//公用规格ID
												$proAtt->zpa_val = $val;		//规格值
												if(!$proAtt->save()){
													$error[] = "ProAttribute $val save fail,";
												}
											}
										}
									}
									
									//保存产品规格
									if(isset($_POST['sta'])){
										$sta = $_POST['sta'];
										foreach($sta as $key=>$val){
											if(is_array($val)){
												//如果规格有多个值，分开单条保存
												foreach($val as $key2=>$val2){
													//$val = serialize($val);
													$proAtt = new ProAttribute;
													$proAtt->zp_id = $model->zp_id;//产品ID
													$proAtt->zpa_type = '2';		//类型为1属性
													$proAtt->za_id = $key;			//公用规格ID
													$proAtt->zpa_val = $val2;		//规格值
													$proSta->zps_photo = (isset($_POST['stacolor'][$key]))?'2':'1';
													//保存规格图片
													if(isset($_POST['stacolor'][$key][$val2])){
														$proAtt->zpa_pic = $_POST['stacolor'][$key][$val2];
														$proAtt->zps_photo ='2';
													}
													if(!$proAtt->save()){
														$error[] = "ProAttribute $val2 save fail,";
													}
												}
											}else{
												//$val = serialize($val);
												$proAtt = new ProAttribute;
												$proAtt->zp_id = $model->zp_id;//产品ID
												$proAtt->zpa_type = '2';		//类型为1属性
												$proAtt->za_id = $key;			//公用规格ID
												$proAtt->zpa_val = $val;		//规格值
												if(!$proAtt->save()){
													$error[] = "ProAttribute $val save fail,";
												}
											}
										}
										//保存规格组合
										$attsta = $_POST['attsta'];
										//重新排序
										sort($sta);
										if(count($sta)==2){
											//有两组规格的时候
											$list[]='';
											$i=0;
											foreach($sta as $key=>$val){
												$sta[$i] = $val;
												$i++;
											}
											foreach($sta[0] as $k1=>$v1){
												foreach($sta[1] as $k2=>$v2 ){
													$list[] = $v1.','.$v2; 
													$list[] = $v2.','.$v1;
												}
											}
											
											$num = count($attsta['val']);
											for($i=0;$i<$num;$i++){
												if(in_array($attsta['val'][$i],$list)){
													$proSta = new ProAttribute;
													$proSta->zp_id = $model->zp_id;//产品ID
													$proSta->zpa_type = '3';		//类型为3规格组合
													$proSta->zpa_val = $attsta['val'][$i];		//规格值
													$proSta->zpa_stock = $attsta['num'][$i];
													$proSta->zpa_pro_no = $attsta['no'][$i];
													$proSta->zps_md5 = md5($model->zp_id.$attsta['val'][$i]);
													if(!$proSta->save()){
														$error[] = "ProSta ".$proSta->zpa_val." save fail,";
													}
													
													//保存产品价格
													$proSpe = new ProSpe;
													$proSpe->zp_id = $model->zp_id;//产品ID
													$proSpe->zpa_id = $proSta->zpa_id;		//规格记录ID
													$proSpe->zps_price = $attsta['price'][$i];	//价格
													if(!$proSpe->save()){
														$error[] = "ProSpe ".$attsta['price'][$i]." save fail,";
													}
												}
											}
										}elseif(count($sta)==1){
											//只有一组规格
											$list= reset($sta);
											$num = count($attsta['val']);
											for($i=0;$i<$num;$i++){
												if(in_array($attsta['val'][$i],$list)){
													$proSta = new ProAttribute;
													$proSta->zp_id = $model->zp_id;//产品ID
													$proSta->zpa_type = '3';		//类型为3规格组合
													$proSta->zpa_val = $attsta['val'][$i];		//规格值
													$proSta->zpa_stock = $attsta['num'][$i];
													$proSta->zpa_pro_no = $attsta['no'][$i];
													$proSta->zps_md5 = md5($model->zp_id.$proSta->zpa_val);
													if(!$proSta->save()){
														$error[] = "ProSta ".$proSta->zpa_val." save fail,";
													}
										
													//保存产品价格
													$proSpe = new ProSpe;
													$proSpe->zp_id = $model->zp_id;//产品ID
													$proSpe->zpa_id = $proSta->zpa_id;		//规格记录ID
													$proSpe->zps_price = $attsta['price'][$i];	//价格
													if(!$proSpe->save()){
														$error[] = "ProSpe ".$attsta['price'][$i]." save fail,";
													}
												}
											}
										}
									}
									//如果使用产品积分系统
									if(F::setting('pointsys')=='2'){
										//保存积分体系
										if(isset($_POST['ProPoint'])){
											foreach($_POST['ProPoint'] as $key=>$val){
												$propoint = ProPoint::model()->findByAttributes(array('zp_id'=>$model->zp_id,'zmt_id'=>$key));
												if(empty($propoint)){
													$propoint = new ProPoint;
													$propoint->zp_id = $model->zp_id;
													$propoint->zmt_id = $key;
												}
												$propoint->zpn_point = $val;//赠送积分												
												if(!$propoint->save()){
													$error[] = "propoint $val save fail,";
												}
											}
										}
									}									
									//删除旧的产品图片记录
									$criteria=new CDbCriteria;
									$criteria->compare('zp_id',$model->zp_id);
									ProImages::model()->deleteAll($criteria);
									//保存产品图片
									$image = $_POST['proimage'];
									foreach($image['url'] as $key=>$val){
										if(!empty($val)){
											$proImages = new ProImages;
											$proImages->zp_id = $model->zp_id;//产品ID
											$proImages->zpg_url = $val;
											$proImages->zpg_listorder = $key;
											$proImages->zta_id =  $image['id'][$key];
											if(!$proImages->save()){
												$error[] = "proimage $val save fail,";
											}
										}
									}
									
									//删除旧的产品自定义分类信息记录
									$criteria=new CDbCriteria;
									$criteria->compare('zp_id',$model->zp_id);
									CmpCatlog::model()->deleteAll($criteria);
									//保存自定义分类信息
									if(isset($_POST['companyCat'])){
										$cmpcat = $_POST['companyCat'];
									}else{
										//如果没选择自定义分类，则保存为未分类产品
										$cmpcat = array('0');
									}
									foreach($cmpcat as $val){
										$cmpcat = new CmpCatlog;
										$cmpcat->zpc_id = $val;
										$cmpcat->zm_id = Yii::app()->user->userid;
										$cmpcat->zp_id = $model->zp_id;//产品ID
										$cmpcat->inputtime = time();
										if(!$cmpcat->save()){
											$error[] = "cmpcat $val save fail,";
										}
									}
									//提交事务
									$transaction->commit();
									$this->render('../index/success',array(
												'message'=>'恭喜，宝贝信息修改成功',
												'links'=>array(
														array('返回商品管理页面',$this->pre_module_url.'/productAll/admin'),
														array('浏览发布的产品','/home/product/detail/id/'.$model->zp_id),
														),
									));
									exit;
						}
						catch(Exception $e){
									//回滚事务
									$transaction->rollback();
									$this->render('../index/error',array(
											'message'=>'修改商品操作失败',
											//'errorlog'=>join(',',$error),
											'links'=>array(
													array('返回商品管理页面',$this->pre_module_url.'/productAll/admin'),
													array('浏览发布的产品','/home/product/detail/id/'.$model->zp_id),
											),
									));
									exit;
						}
		}
		
		//渲染视图
		$this->render('product_tow',array(
						   'model'=>$model,
		));
	}

	/**
	 * 发布预览
	 */
	public function actionEditView(){
		//实例化产品模型
		$model=new Product;
		$model->zca_id = (int)$_POST['Product']['zca_id'];
		//声明模块为前台
		$this->pre_module_url = "/home";
			
		//提交发布产品操作
		if(isset($_POST['Product']))
		{
			//赋值产品基本信息
			$model->attributes=$_POST['Product'];
			$model->zp_pic = $_POST['proimage']['url'][0];//产品默认图片
		
			//保存产品属性
			if(isset($_POST['ProAttribute'])){
				$att = $_POST['ProAttribute'];
				foreach($att as $key=>$val){
					if(is_array($val)){
						//如果属性有多个值，分开单条保存
						foreach($val as $key2=>$val2){
							$proAtt = new ProAttribute;
							$proAtt->zp_id = $model->zp_id;//产品ID
							$proAtt->zpa_type = '1';		//类型为1属性
							$proAtt->za_id = $key;			//公用规格ID
							$proAtt->zpa_val = $val2;		//规格值
							$proatt[] = $proAtt;
						}
					} else{
						//$val = serialize($val);
						$proAtt = new ProAttribute;
						$proAtt->zp_id = $model->zp_id;//产品ID
						$proAtt->zpa_type = '1';		//类型为1属性
						$proAtt->za_id = $key;			//公用规格ID
						$proAtt->zpa_val = $val;		//规格值
						$proatt[] = $proAtt;
					}
				}
			}
									
			//保存产品规格
			if(isset($_POST['sta'])){
				$sta = $_POST['sta'];
				foreach($sta as $key=>$val){
					if(is_array($val)){
						//如果规格有多个值，分开单条保存
						foreach($val as $key2=>$val2){
							//$val = serialize($val);
							$proAtt = new ProAttribute;
							$proAtt->zp_id = $model->zp_id;//产品ID
							$proAtt->zpa_type = '2';		//类型为1属性
							$proAtt->za_id = $key;			//公用规格ID
							$proAtt->zpa_val = $val2;		//规格值
							$proSta->zps_photo = (isset($_POST['stacolor'][$key]))?'2':'1';
							//保存规格图片
							if(isset($_POST['stacolor'][$key][$val2])){
								$proAtt->zpa_pic = $_POST['stacolor'][$key][$val2];
								$proAtt->zps_photo ='2';
							}
							$prospe[] = $proAtt;
						}
					}else{
						//$val = serialize($val);
						$proAtt = new ProAttribute;
						$proAtt->zp_id = $model->zp_id;//产品ID
						$proAtt->zpa_type = '2';		//类型为1属性
						$proAtt->za_id = $key;			//公用规格ID
						$proAtt->zpa_val = $val;		//规格值
						$prospe[] = $proAtt;
					}
				}
				//保存规格组合
				$attsta = $_POST['attsta'];
				if(count($sta)==2){
					//有两组规格的时候
					$list[]='';
					$i=0;
					foreach($sta as $key=>$val){
						$sta[$i] = $val;
						$i++;
					}
					foreach($sta[0] as $k1=>$v1){
						foreach($sta[1] as $k2=>$v2 ){
							$list[] = $v1.','.$v2;
						}
					}
					$k=1;
					$num = count($attsta['val']);
					for($i=0;$i<$num;$i++){
						if(in_array($attsta['val'][$i],$list)){
							$proSta = new ProAttribute;
							$proSta->zp_id = $model->zp_id;//产品ID
							$proSta->zpa_type = '3';		//类型为3规格组合
							$proSta->zpa_val = $attsta['val'][$i];		//规格值
							$proSta->zpa_stock = $attsta['num'][$i];
							$proSta->zpa_pro_no = $attsta['no'][$i];
							$proSta->zps_md5 = md5($model->zp_id.$attsta['val'][$i]);
							//赋值产品规格库存
							$spec[] = $proSta;
							
							//保存产品价格
							$proSpe = new ProSpe;
							$proSpe->zp_id = $model->zp_id;//产品ID
							$proSpe->zpa_id = $proSta->zpa_id;		//规格记录ID
							$proSpe->zps_price = $attsta['price'][$i];	//价格
							//赋值产品价格
							$price[] = $proSpe;
							$k++;
						}
					}
				}elseif(isset($sta[0])){
					//只有一组规格
					$list[]=$sta[0];
					$colornum = count($attsta['color']);
					$k=1;
					$num = count($attsta['val']);
					for($i=0;$i<$num;$i++){
						if(in_array($attsta['val'][$i],$list)){
							$proSta = new ProAttribute;
							$proSta->zp_id = $model->zp_id;//产品ID
							$proSta->zpa_type = '3';		//类型为3规格组合
							$proSta->zpa_val = $attsta['val'][$i];		//规格值
							$proSta->zpa_stock = $attsta['num'][$i];
							$proSta->zpa_pro_no = $attsta['no'][$i];
							if(!empty($sta['color'][$i])){
								$proSta->zps_photo ='2';
							}
							$proSta->zps_md5 = md5($model->zp_id.$proSta->zpa_val);
							//赋值产品规格库存
							$spec[] = $proSta;
							
								
							//保存产品价格
							$proSpe = new ProSpe;
							$proSpe->zp_id = $model->zp_id;//产品ID
							$proSpe->zpa_id = $proSta->zpa_id;		//规格记录ID
							$proSpe->zps_price = $attsta['price'][$i];	//价格
							//赋值产品价格
							$price[] = $proSpe;
							$k++;
						}
					}
				}
			}
			//如果使用产品积分系统
			if(F::setting('pointsys')=='2'){
				//保存积分体系
				if(isset($_POST['ProPoint'])){
					foreach($_POST['ProPoint'] as $key=>$val){
						$propoint = new ProPoint();
						$propoint->zp_id = $model->zp_id;//产品ID
						$propoint->zmt_id = $key;//会员组ID
						$propoint->zpn_point = $val;//赠送积分
						$productinfo['point'][] = $propoint;
					}
				}
			}
		
			//保存产品图片
			$image = $_POST['proimage'];
			foreach($image['url'] as $key=>$val){
				if(!empty($val)){
					$proImages = new ProImages;
					$proImages->zp_id = $model->zp_id;//产品ID
					$proImages->zpg_url = $val;
					$proImages->zpg_listorder = $key;
					$proImages->zta_id =  $image['id'][$key];
					//取产品图片 
					$proimg[] = $proImages;
				}
			}
		
			//保存自定义分类信息
			if(isset($_POST['companyCat'])){
				$cmpcat = $_POST['companyCat'];
			}else{
				//如果没选择自定义分类，则保存为未分类产品
				$cmpcat = array('0');
			}
			foreach($cmpcat as $val){
				$cmpcat = new CmpCatlog;
				$cmpcat->zpc_id = $val;
				$cmpcat->zm_id = Yii::app()->user->userid;
				$cmpcat->zp_id = $model->zp_id;//产品ID
				$cmpcat->inputtime = time();
				//自定义分类  
				$cmpcats[] = $cmpcat;
				
			}
			$productinfo = $model->attributes;
			
			//处理产品规格
			if($prospe){
				foreach($prospe as $val){
					$productinfo['spe'][$val->za_id]['za_id']=$val->za_id;
					$za_title = Attribute::model()->findByAttributes(array('za_id'=>$val->za_id));
					if(!empty($za_title)){
						$productinfo['spe'][$val->za_id]['za_title']=$za_title->attributes['za_title'];
					}
					$productinfo['spe'][$val->za_id]['zpa_val'][]=$val->attributes;
					$productinfo['spe'][$val->za_id]['za_id']=$val->za_id;
					$productinfo['spe'][$val->za_id]['zps_photo']=$val->zps_photo;
				}
			}else{
				$productinfo['spe'] = array();
			}
		//	print_r($productinfo['spe']);exit;
			//处理产品规格属性
			if($att){
				foreach($att as $key=>$val){
					$productinfo['att'][$key]['za_id']=$key;
					$za_title = Attribute::model()->findByAttributes(array('za_id'=>$key));
					if(!empty($za_title)){
						$productinfo['att'][$key]['za_title']=$za_title->attributes['za_title'];
					}
					$productinfo['att'][$key]['zpa_val']=$val;
				}
			}else{
				$productinfo['att'] = array();
			}
			
			//处理图片
			if($proimg){
				foreach($proimg as $val){
					$productinfo['imgs'][] = $val->attributes;
				}
			}
		//	print_r($productinfo['img']);
			//渲染视图
			$this->layout = "//layouts/home";
    		$this->render('../../home/product/detail',
    			array(
    				'productinfo'=>$productinfo,
    	//			'ajaxspe'=>$ajaxspe,  				
    				'category'=>Category::model()->GetCatCache(),
    				'editView'=>true,
    				)
    		);
		}
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Product::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}


}
